precision mediump float;

uniform sampler2D u_sampler2D;
uniform sampler2D u_samplerNormalMap;
uniform vec3 u_lightPosition;

varying vec3 v_normalW;
varying vec3 v_bitangentW;
varying vec3 v_tangentW;
varying vec2 v_uv;

void main()
{
    vec3 texMap = texture2D(u_samplerNormalMap, v_uv).xyz;
    mat3 TBN = mat3(v_tangentW, v_bitangentW, v_normalW);
    vec3 texMapW = normalize(TBN * (2.0 * texMap - 1.0));
    float diffuse = max(dot(normalize(u_lightPosition), texMapW), 0.0);

    vec4 tex = texture2D(u_sampler2D, v_uv);
    gl_FragColor = vec4(diffuse * tex.xyz, tex.a);
}
